<template>
  <div class="user-add-main">
    <el-form ref="addForm" :model="adduser" clearable filterable label-width="90px">
      <el-form-item label="姓名:" prop="name" :rules="addRules.name">
        <el-input v-model="adduser.name" />
      </el-form-item>
      <el-form-item label="手机号码:" prop="phone" :rules="addRules.phone">
        <el-input v-model="adduser.phone"  />
      </el-form-item>
      <el-form-item label="密码:" prop="password" :rules="addRules.password">
        <el-input v-model="adduser.password" type="password"/>
      </el-form-item>
      <el-form-item label="岗位:" prop="jobsId" :rules="addRules.jobsId">
        <el-select v-model="adduser.jobsId" placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.jobsName"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="头像:">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="header"
          :on-success="handleAvatarSuccess">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import userApi from '@/api/users'
import { getToken } from '@/utils/auth'
import md5 from 'js-md5'
export default {
  data() {
    return {
      adduser:{
        headerUrl: "",
        jobsId: null,
        name: "",
        password: "",
        phone: "",
      },
      addRules: {
        name: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*姓名不能为空!'))
                } else if(value.length<2){
                  callback(new Error('*请输入正确姓名!'))
                } else {
                  this.$refs['addForm'].clearValidate(['name']);
                  callback()
                }
              }
        }],
        phone: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*手机号码不能为空!'))
                } else if(value.length!=11){
                  callback(new Error('*请输入正确手机号!'))
                } else {
                  this.$refs['addForm'].clearValidate(['phone']);
                  callback()
                }
              }
        }],
        jobsId: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value==null) {
                  callback(new Error('*岗位不能为空!'))
                } else {
                  this.$refs['addForm'].clearValidate(['jobsId']);
                  callback()
                }
              }
        }],
        password: [{ type: 'String', required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*密码不能为空!'))
                } else if (value.length < 6) {
                  callback(new Error('密码长度不能小于6位!'))
                } else {
                  this.$refs['addForm'].clearValidate(['password']);
                  callback()
                }
              }
        }],
      },
      typeList: this.$store.getters.typeList,
      header: {
        Authorization: getToken()
      },
      imageUrl: "", // 图片回显路径
      uploadUrl: process.env.VUE_APP_ADMIN_URL //  图片上传路径
    }
  },
  watch: {
    'adduser.headerUrl': function(newVal, oldVal) {
      this.imageUrl = newVal
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.$message.success(res.msg)
      if(res.msg!="上传失败！"){
        this.imageUrl = res.data
        this.adduser.headerUrl = res.data
      }
    },
    onSubmit() {
        this.$refs.addForm.validate(valid => {
        if (valid) {
          this.adduser.password = md5(this.adduser.password)
          userApi.addUser(this.adduser).then(res => {
            this.$message.success(res.data)
            this.$emit('closeDisplay')
            this.$emit('getPageList')
            this.$refs.addForm.resetFields()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .user-add-main{
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
